<html>
  <head>
    <meta charset="UTF-8" />
    <title>spine-pixi-v8</title>
    <script src="https://cdn.jsdelivr.net/npm/pixi.js@8.4.1/dist/pixi.js"></script>
    <script src="../dist/iife/spine-pixi-v8.js"></script>
    <link rel="stylesheet" href="../../index.css">
  </head>

  <body>
    <!-- Creates a transparent logging overlay. -->
    <div id="log" class="overlay" style="user-select: none;" max-height: 300px; overflow: auto;>
    </div>

    <script>
      function log(message) {
        const log = document.querySelector("#log");
        log.innerText += message + "\n";
        log.scrollTop = log.scrollHeight;
        console.log(message);
      }

      (async function () {
        var app = new PIXI.Application();
        await app.init({
          width: window.innerWidth,
          height: window.innerHeight,
          resolution: window.devicePixelRatio || 1,
          autoDensity: true,
          resizeTo: window,
          backgroundColor: 0x2c3e50,
          hello: true,
        })
        document.body.appendChild(app.canvas);

        // Pre-load the skeleton data and atlas. You can also load .json skeleton data.
        PIXI.Assets.add({alias: "spineboyData", src: "/assets/spineboy-pro.skel" });
        PIXI.Assets.add({alias: "spineboyAtlas", src: "/assets/spineboy-pma.atlas" });
        await PIXI.Assets.load(["spineboyData", "spineboyAtlas"]);

        // Create the Spine display object
        const spineboy = spine.Spine.from({skeleton: "spineboyData", atlas: "spineboyAtlas",
          scale: 0.5,
        });

        // Set animation "run" on track 0, looped.
        spineboy.state.setAnimation(0, "run", true);

         // Set callbacks to receive animation state events.
         spineboy.state.addListener({
          start: (entry) => log(`Started animation ${entry.animation.name}`),
          interrupt: (entry) => log(`Interrupted animation ${entry.animation.name}`),
          end: (entry) => log(`Ended animation ${entry.animation.name}`),
          dispose: (entry) => log(`Disposed animation ${entry.animation.name}`),
          complete: (entry) => log(`Completed animation ${entry.animation.name}`),
        });

        // Add a custom event listener along with an
        // unlooped animation to see the custom event logged.
        const trackEntry = spineboy.state.addAnimation(0, "walk", false, 3);
        trackEntry.listener = {
          event: (entry, event) =>
          log(`Custom event for ${entry.animation.name}: ${event.data.name}`),
        };

        spineboy.state.addAnimation(0, "run", true, 0);

        // Set the default mix time to use when transitioning
        // from one animation to the next.
        spineboy.state.data.defaultMix = 0.2;

        // Center the spine object on screen.
        spineboy.x = window.innerWidth / 2;
        spineboy.y = window.innerHeight / 2 + spineboy.getBounds().height / 2;

        // Add the display object to the stage.
        app.stage.addChild(spineboy);
      })();
    </script>
  </body>
</html>
